package com.dongpl.module.system_user.controller;

import cn.hutool.core.util.StrUtil;
import com.dongpl.module.system_user.model.SystemUser;
import com.dongpl.module.system_user.service.SystemUserService;
import com.dongpl.utils.VerifyCodeUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sys_user")
public class SystemUserController {

    @Resource
    private SystemUserService service;

    @RequestMapping("login")
    public Map<String, Object> login(@RequestBody SystemUser user, HttpSession session){
        Map<String, Object> result = new HashMap<>();
        String username = user.getUsername();
        String password = user.getPassword();
        String checkCode = user.getCheckCode();
        if (StrUtil.isEmpty(username)){
            result.put("status",false);
            result.put("msg","用户名不能为空！");
            return result;
        }
        if (StrUtil.isEmpty(password)){
            result.put("status",false);
            result.put("msg","密码不能为空！");
            return result;
        }
        if (StrUtil.isEmpty(checkCode)){
            result.put("status",false);
            result.put("msg","验证码不能为空！");
            return result;
        }
        // 比较验证码
        String codes = (String) session.getAttribute("code");
        if (checkCode.equalsIgnoreCase(codes)){
            // 获取主体对象
            result.put("status",true);
            result.put("msg","登录成功！");
        } else {
            result.put("status",false);
            result.put("msg","验证码错误！");
        }
        return result;
    }

    @RequestMapping("register")
    public Map<String, Object> register(@RequestBody SystemUser user){
        Map<String, Object> result = new HashMap<>();
        String username = user.getUsername();
        if (StrUtil.isEmpty(username)){
            result.put("status",false);
            result.put("msg","用户名不能为空！");
            return result;
        }
        String password = user.getPassword();
        if (StrUtil.isEmpty(password)){
            result.put("status",false);
            result.put("msg","密码不能为空！");
            return result;
        }
        String pass = user.getPass();
        if (!password.equals(pass)){
            result.put("status",false);
            result.put("msg","两次密码不一致！");
            return result;
        }
        Boolean aBoolean = service.registerUser(user);
        if (aBoolean){
            result.put("status",true);
            result.put("msg","注册成功！");
        } else {
            result.put("status",false);
            result.put("msg","注册失败！");
        }
        return result;
    }

    @RequestMapping("/logout")
    public Map<String, Object> logout(){
        Map<String, Object> result = new HashMap<>();
        try{
            result.put("status",true);
            result.put("msg","退出成功！");
        } catch (Exception ignored){
            result.put("status",false);
            result.put("msg","退出失败！");
            return result;
        }
        return result;
    }

    /**
     * 获取验证码
     */
    @RequestMapping("getImage")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        // 生成验证码
        String code = VerifyCodeUtils.generateVerifyCode(4);
        // 验证码放入session
        session.setAttribute("code",code);
        // 验证码存入图片
        ServletOutputStream os = response.getOutputStream();
        // 刷新输出流
        os.flush();
        response.setContentType("image/png");
        VerifyCodeUtils.outputImage(220,60,os,code);
    }

}
